package com.gugee.tiktok.common.model.mysql;

import cn.hutool.core.date.DateUtil;
import com.gugee.tiktok.common.model.spider.aweme.Anchor;
import com.gugee.tiktok.common.model.spider.aweme.Aweme_list;
import com.gugee.tiktok.common.model.spider.aweme.Text_extra;
import com.xiguaji.boot.data.rdb.aggregate.Aggregate;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.boot.toolkit.MapperUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("aweme")
public class Aweme extends Aggregate {
    /**
     * 作品Id
     */
    private long oAwemeId;
    /**
     * 作品说明
     */
    private String oDesc;
    /**
     * 创建时间
     */
    private long oCreateTime;
    /**
     * 用户id
     */
    private long oUid;
    /**
     * 音乐id
     */
    private long oMusicId;
    /**
     * 评论数
     */
    private int oCommentCount;
    /**
     * 点击数
     */
    private long oDiggCount;
    /**
     * 下载数
     */
    private int oDownloadCount;
    /**
     * 播放数
     */
    private long oPlayCount;
    /**
     * 分享数
     */
    private int oShareCount;
    /**
     * Whatsapp 分享数量
     */
    private int oWhatsappShareCount;
    /**
     * 评分
     */
    private int oRate;
    /**
     * 标签
     */
    private String oTextExtra;
    /**
     * 分享链接
     */
    private String oShareUrl;
    /**
     * 视频长度 秒
     */
    private int oDuration;
    /**
     * 区域
     */
    private String oRegion;
    /**
     * 是否广告
     */
    private boolean oIsAds;

    private int oForwardCount;

    private String oBloggerNickName;

    private String oBloggerUniqueId;

    private String oCover;

    private String oDynamicCover;

    private int datecode;

    private double score;

    private String bloggerType;
    private String awemeType;
    /**
     * 已分析关键词的评论数
     */
    public int analysisComments;
    /**
     * 已采集评论数
     */
    public int synComments;
    /**
     * 锚点信息 json结构
     */
    public String anchor;
    /**
     * 视频分类依据来源 视频标签：1 模型：0
     */
    private int awemeTypeSource;
    /**
     * 视频分类时使用的评论数
     */
    private int awemeTypeComments;
    /**
     * 最近视频分类时间
     */
    private long lastAwemeTypeTime;
    @Mapper(componentModel = "spring", imports = {DateTimeUtils.class, MapperUtils.class})
    public interface AwemeMapper {
        List<Aweme> mapper(List<Aweme_list> list);

        @Mapping(source = "aweme_id", target = "oAwemeId")
        @Mapping(source = "desc", target = "oDesc")
        @Mapping(source = "create_time", target = "oCreateTime")
        @Mapping(source = "author.uid", target = "oUid")
        @Mapping(source = "music.mid", target = "oMusicId")
        @Mapping(source = "statistics.comment_count", target = "oCommentCount")
        @Mapping(source = "statistics.digg_count", target = "oDiggCount")
        @Mapping(source = "statistics.download_count", target = "oDownloadCount")
        @Mapping(source = "statistics.play_count", target = "oPlayCount")
        @Mapping(source = "statistics.share_count", target = "oShareCount")
        @Mapping(source = "statistics.forward_count", target = "oForwardCount")
        @Mapping(source = "statistics.whatsapp_share_count", target = "oWhatsappShareCount")
        @Mapping(source = "rate", target = "oRate")
        @Mapping(target = "oTextExtra", expression = "java(text_extra(dto.getText_extra()))")
        @Mapping(source = "share_url", target = "oShareUrl")
        @Mapping(source = "duration", target = "oDuration")
        @Mapping(source = "region", target = "oRegion")
        @Mapping(source = "author.nickname", target = "oBloggerNickName")
        @Mapping(source = "author.unique_id", target = "oBloggerUniqueId")
        @Mapping(target = "oCover", expression = "java(getCover(dto))")
        @Mapping(target = "oDynamicCover", expression = "java(getDynamicCover(dto))")
        @Mapping(target = "datecode", expression = "java(getDateCode(dto))")
        @Mapping(target = "anchor", expression = "java(anchor(dto.getAnchor()))")
        Aweme mapper(Aweme_list dto);
        default String anchor(Anchor anchor) {
            if(anchor==null){
                return null;
            }
            return JsonUtil.parseJson(anchor);
        }
        default String text_extra(List<Text_extra> text_extras) {
            try {
                if (CollectionUtils.isEmpty(text_extras)) {
                    return "";
                }
                List<Hashtag> hashtagList =new ArrayList<>(text_extras.size());
                for(Text_extra textExtra:text_extras){
                    if(textExtra.getType()==0){
                        continue;
                    }
                    Hashtag hashTag=new Hashtag();
                    hashTag.setCommerce(textExtra.isCommerce());
                    hashTag.setHashtagName(textExtra.getHashtag_name());
                    hashTag.setHid(textExtra.getHashtag_id());
                    hashTag.setType(textExtra.getType());
                    hashtagList.add(hashTag);
                }
                return MapperUtils.writeValue(
                        hashtagList
                );
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        default String getCover(Aweme_list dto) {
            List<String> urls=dto.getVideo().getCover().getUrl_list();

            if (CollectionUtils.isEmpty(urls)) return "";

            for(String url: urls){
                //目前有jpeg、webp两种格式，确保万一，只排除webp格式，
                if(url.indexOf("webp")>0){
                    continue;
                }
                return url;
            }

            return "";
        }

        default String getDynamicCover(Aweme_list dto) {
            if (CollectionUtils.isEmpty(dto.getVideo().getDynamic_cover().getUrl_list())) return "";
            return dto.getVideo().getDynamic_cover().getUrl_list().get(0);
        }

        default int getDateCode(Aweme_list dto){
            return Integer.parseInt(DateUtil.format(DateTimeUtils.localDateTimeBySecond(dto.getCreate_time()),"yyyyMMdd"));

        }
    }
}
